	

    package edu.c06190115.cardiac;
     
    import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
     
    import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
     
    public class FileRead {
     
            public Step[] stepArr = new Step[100];
     
            public class Step {
                    int isChoice;
                    int numOfSteps;
                    int hasTimer;
                    int prevStep;
                    int nextStep;
                    int noNextStep;
                    String stepInfo;
                    //private Step next;
     
                    public Step() {
                            hasTimer = 1;
                            prevStep = 0;
                            //next = null;
                    }
     
                    protected Step(String[] theData) {
                            for(int i = 0; i < theData.length; i++) {
                                    System.out.println("the data at " + i + " " + theData[i]);
                            }
                            //stepType -> isChoice
                            //hasTimer -> hasTimer
                            //choiceNextStep -> noNextStep
                            isChoice = Integer.parseInt(theData[0]);
                            hasTimer = Integer.parseInt(theData[1]);
                            prevStep = Integer.parseInt(theData[2]);
                            nextStep = Integer.parseInt(theData[3]);
                            noNextStep = Integer.parseInt(theData[4]);
                            numOfSteps = Integer.parseInt(theData[5]);
                            stepInfo = theData[6];
                    }
     
                    public String toString() {
                            return "Step info is : " + stepInfo;
                    }
     
                    public int hasChoice() {
                            return isChoice;
                    }
     
                    public int hasTimer() {
                            return hasTimer;
                    }
     
                    public int getNextStep() {
                            return nextStep;
                    }
     
                    public int getPrevStep() {
                            return prevStep;
                    }
     
                    public int getNoNext() {
                            return noNextStep;
                    }
     
                    public int getNumOfSteps() {
                            return numOfSteps;
                    }
     
                    public String getStepInfo() {
                            return stepInfo;
                    }
     
     
     
                    //closes the Step class
            }
     
            public FileRead (String algorithmName, Context ctx) {
                    int counter = 0;
     
                    //Set the size of the amount of possible steps**********************
                    //Step[] stepArr = new Step[100];
     
                    try{
                            //Open the file corresponding to algorithmName
                            InputStream inputStream;
                            if (algorithmName.equals("Asystole, Pediatric")) {
                                    inputStream = ctx.getResources().openRawResource(R.raw.pediatricasystoleandpea);
                            } else if (algorithmName.equals("Asystole, Adult")) {
                                inputStream = ctx.getResources().openRawResource(R.raw.adultasystole);
                            } else if (algorithmName.equals("PEA, Adult")) {
                            	 inputStream = ctx.getResources().openRawResource(R.raw.adultpea);
                            } else if (algorithmName.equals("V-Tach with Pulse, Adult")) {
                            	inputStream = ctx.getResources().openRawResource(R.raw.adultvtachwithpulse);
                            } else if (algorithmName.equals("Bradycardia with Pulse, Adult")) {
                            	inputStream = ctx.getResources().openRawResource(R.raw.bradycardiawithpulse);
                            } else if (algorithmName.equals("Bradycardia with Pulse, Pediatric")) {
                            	inputStream = ctx.getResources().openRawResource(R.raw.pedbradycardiawithpulse);
                            } else if (algorithmName.equals("V-Tach with Pulse, Pediatric")) {
                            	inputStream = ctx.getResources().openRawResource(R.raw.pedtachycardiawithpulses);
                            } else if (algorithmName.equals("V-fib and Pulseless V-tach, Pediatric")) {
                            	inputStream = ctx.getResources().openRawResource(R.raw.pediatricvfibvtach);
                            } else if (algorithmName.equals("V-Fib, Adult")) {
                            	inputStream = ctx.getResources().openRawResource(R.raw.adultvfib);
                            } else {
                                inputStream = ctx.getResources().openRawResource(1);
                            }
                            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
                            String strLine;
                            //Read File Line By Line
                            while ((strLine = br.readLine()) != null) {
                                    if(strLine.startsWith("-")){
                                            System.out.println("This worked");
                                            stepArr = StringParser(counter, stepArr, strLine);
                                    }
     
                                    counter++;
                                    // Print the content on the console
                                    System.out.println (strLine);
                            }
                            //Close the input stream
                            inputStream.close();
                    } catch (Exception e){//Catch exception if any
                            System.err.println("Error: " + e.getMessage());
                    }
     
                    //checking the stepArr array
                    for (int i = 0; i < 7; i++) {
                            System.out.println("The objects in the array at " + i + " are " + stepArr[i]);
                    }
     
                    //closes constructor FileRead
            }
     
            Step[] StringParser(int counter, Step[] stepArr, String theLine) {
                    System.out.println("Parsing Strings");
                    System.out.println("the string is " + theLine);
                    //change the size of the String array to be stored in the object******
                    String[] theData = new String[7];
     
                    //try {
                    //Thread.sleep(1000);
                    theData[0] = theLine.substring(1,2);
                    theData[1] = theLine.substring(2,3);
                    theData[2] = theLine.substring(4,6);
                    theData[3] = theLine.substring(7,9);
                    theData[4] = theLine.substring(10,12);
                    theData[5] = theLine.substring(13,14);
                    theData[6] = theLine.substring(15);
                    Step theStep = new Step(theData);
     
                    stepArr[counter] = theStep;
     
                    //} catch(InterruptedException ex) {
                    //  Thread.currentThread().interrupt();
                    //}
                    //it failed return
                    return stepArr;
     
            }
     
            //closes public class FileRead
    }

